<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button @click="isclose=!isclose" color="red" class="edit-btn" round plain type="primary"
        >{{isclose ? '完成':'编辑'}}</van-button
      >
    </van-cell>

    <van-grid :gutter="10" :border="false" class="my-grid">
      <van-grid-item
      :class="{active:index===active}"
        :text="item.name"
        :icon="isclose && index!==0 ? 'close':''"
        v-for="(item,index) in channels"
        :key="item.id"
        @click="onMyClick(index,item.id)"
        />

    </van-grid>

    <van-cell title="推荐频道" :border="false"> </van-cell>

    <van-grid
      direction="horizontal"
      :gutter="10"
      :border="false"
      class="my-grid"
      icon-size='0.3rem'
    >
      <van-grid-item @click="onRecommendChannels(item)" v-for="item in recommendChannels" :key="item.id" icon="plus" :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels } from '@/api/article'
import { addUserSelfChannels, delUserSelfChannels } from '@/api/user.js'
import { Toast } from 'vant'
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage.js'
export default {
  name: 'channels',
  props: {
    channels: {
      type: Array,
      require: true
    },
    active: {
      type: Number,
      require: true
    }

  },
  data () {
    return {
      isclose: false,
      allchannels: []
    }
  },

  created () {
    this.loadChannel()
  },

  methods: {
    // 如果isclose处于编辑状态即true 就 点击每一项 删除 否则跳转
    onMyClick (index, id) {
      if (this.isclose) {
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('setActive')
        }
        this.channels.splice(index, 1)

        this.delChannelsStorage(id)
      } else {
        this.$emit('onGo', index)
      }
    },
    async loadChannel () {
      try {
        const res = await getAllChannels()
        this.allchannels = res.channels
      } catch (err) {
        console.dir(err)
        Toast('获取失败')
      }
    },

    // 删除我的频道数据持久化
    async delChannelsStorage (id) {
      // 未登录
      if (!this.user) return setStorage('MY-CHANNELS', this.channels)

      // 加载中
      Toast.loading({
        message: '删除...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })

      // 已登录状态
      try {
        await delUserSelfChannels(id)
        Toast.success('删除成功')
      } catch (err) {
        Toast.fail('删除成功')
      }
    },

    // 推荐频道
    async onRecommendChannels (item) {
      this.channels.push(item)

      // 加载中
      Toast.loading({
        message: '添加中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      // 数据持久化
      // 已登录
      if (this.user) {
        try {
          await addUserSelfChannels({
            id: item.id,
            seq: this.channels.length
          })
          Toast.success('添加成功')
        } catch (err) {
          Toast.fail('添加失败')
        }
      } else {
        // 未登录
        setStorage('MY-CHANNELS', this.channels)
        Toast.success('添加成功')
      }
    }
  },
  computed: {
    // 对allChannels进行筛选
    // allChannels的每一项 是否在channels里,如果没有在channels,属于recommendChannels
    // recommendChannels () {
    //   const arr = this.allchannels.filter(item => {
    //     const flag = this.channels.some(item1 => item1.id === item.id)
    //     return !flag
    //   })
    //   return arr
    // }
    ...mapState(['user']),
    recommendChannels () {
      // return this.allchannels.filter(item => {
      //   return !this.channels.some(item1 => item1.id === item.id)
      // })
      console.log(this.channels)
      return this.allchannels.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
    }

  }
}
</script>

<style scoped lang="less">
.channel-edit {
  .edit-btn {
    width: 104px;
    height: 48px;
    padding: 0;
  }
  /deep/.my-grid {
    .van-grid-item {
      width: 160px;
      height: 86px;
      border-radius: 6px;
    }
    .van-grid-item__content {
      background: #f4f5f6;
    }
    .van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -10.5px;
      right: 5px;
    }
    .van-grid-item__icon+.van-grid-item__text {
      margin-top: 0;
    }
  }
  .active{
    color: red;
    /deep/.van-grid-item__text{
      color: red;
    }
  }
}
</style>
